package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallUserGoldcoinRecordMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallUserExample;
import org.linlinjava.litemall.db.domain.LitemallUserGoldcoinRecord;
import org.linlinjava.litemall.db.domain.LitemallUserGoldcoinRecordExample;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallUserGoldcoinRecordService {

    @Resource
    private LitemallUserMapper userMapper;

    @Resource
    private LitemallUserGoldcoinRecordMapper litemallUserGoldcoinRecordMapper;


    @Transactional
    public String addGoldCoin(LitemallUserGoldcoinRecord record) {
        if(record.getDirection().equals(1)){
            userMapper.updateGoldcoinById(record.getGoldcoin(),record.getUserId());
        }else {
            userMapper.updateGoldcoinById(-record.getGoldcoin(),record.getUserId());
        }
        record.setAddTime(LocalDateTime.now());
        record.setUpdateTime(LocalDateTime.now());
        litemallUserGoldcoinRecordMapper.insert(record);
        return "success";
    }

    // 根据用户id查询积分列表
    public List<LitemallUserGoldcoinRecord> queryByIdUserId(Long userId) {
        LitemallUserGoldcoinRecordExample example = new LitemallUserGoldcoinRecordExample();
        LitemallUserGoldcoinRecordExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
//        PageHelper.startPage(page, size);
        return litemallUserGoldcoinRecordMapper.selectByExample(example);
    }


}
